<?php
  include('header.php');
  $hide_footer = 1;
  if ($_SESSION['signed_in'] == false or $_SESSION['user_level'] < 1) {
      //the user is not an admin
      header('location: signin.php');
      echo '<br /><a href="index.php?id=' . mysql_real_escape_string($_GET['p']) . '">' . $l_back_to . '</a>.';
  } else {
      $pageTitle = "$l_moderatorcp";
      
      echo "<hr /><br /><div align='left'>&nbsp; 
    <a class='profile' href='mcp.php?reports=1'>$l_reports</a>
	<a class='profile' href='mcp.php?approved=1'>$l_unnaproved_posts</a>
	<a class='profile' href='mcp.php?approve_topic=1'>$l_unnaproved_topics</a>
    <a class='profile' href=''>$l_user_manage</a>
    <br><br>";
      
      if (isset($_GET['reports']) and $_GET['reports'] == 1) {
	  //START PAGINATION
          $result = $db->query(" SELECT COUNT(*) FROM " . $table_prefix . "post_reports");
          $r = $db->fetch_row($result);
          $numrows = $r[0];
          // number of rows to show per page
          $rowsperpage = $topics_per_page;
          $totalpages = ceil($numrows / $rowsperpage);
          if (isset($_GET['page']) && is_numeric($_GET['page'])) {
              $currentpage = (int)$_GET['page'];
          } else {
              $currentpage = 1;
          }
          if ($currentpage > $totalpages) {
              $currentpage = $totalpages;
          }
          if ($currentpage < 1) {
              $currentpage = 1;
          }
          if ($totalpages <= 1) {
              $totalpages = 1;
          }
          $offset = ($currentpage - 1) * $rowsperpage;
          $current_page = $page;
          if ($current_page == "") {
              $current_page = 1;
          }
          $range = $max_pagination_link;
          if ($range >= $totalpages) {
              $range = $totalpages;
          }
          echo "<table><tr>";
          echo '<td align="right" class="pagination"> <b>'.$l_page.' ' . $current_page . ' '.$l_page_of.' ' . $totalpages . '</b> [' . $numrows . ' '.$l_maches.']  &nbsp; &nbsp;';
          if ($currentpage > 1) {
              echo " <a title= '$l_first_page' href='{$_SERVER['PHP_SELF']}?reports=1&page=1'><<</a> ";
              $prevpage = $currentpage - 1;
              echo " <a title='$l_prev_page' href='{$_SERVER['PHP_SELF']}?reports=1&page=$prevpage'><</a> ";
          }
          for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
              if (($x > 0) && ($x <= $totalpages)) {
                  if ($x == $currentpage) {
                      echo " [<b>$x</b>] ";
                  } else {
                      echo " <a title='$l_goto_page " . $x . "' href='{$_SERVER['PHP_SELF']}?reports=1&page=$x'>$x</a> ";
                  }
              }
          }
          if ($currentpage != $totalpages) {
              $nextpage = $currentpage + 1;
              echo " <a title='$l_next_page' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=$nextpage'>></a> ";
              echo " <a title='$l_goto_last_page' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=$totalpages'>>></a> ";
          }
		  //END PAGINATION
          echo '</td></tr></table>';
          $get_reports = $db->query("SELECT 
              " . $table_prefix . "post_reports.report_id,
              " . $table_prefix . "post_reports.post_id,
              " . $table_prefix . "post_reports.user_id,
              " . $table_prefix . "post_reports.user_name,
              " . $table_prefix . "post_reports.user_ip,
              " . $table_prefix . "posts.post_subject,
              " . $table_prefix . "posts.post_topic,
              " . $table_prefix . "posts.post_page
              FROM 
              " . $table_prefix . "post_reports 
              LEFT JOIN 
              " . $table_prefix . "posts 
              ON 
              " . $table_prefix . "posts.post_id = " . $table_prefix . "post_reports.post_id 
              ORDER BY 
              " . $table_prefix . "posts.post_date DESC LIMIT $offset, $post_per_page");
          echo "<script language=\"JavaScript\">
function toggle(source) {
  checkboxes = document.getElementsByName('checkbox[]');
  for(var i in checkboxes)
    checkboxes[i].checked = source.checked;
}
</script>
<form name ='myform' action='' method='post'>		  
		  <table border=1><tr><th><b><input type='checkbox' onClick='toggle(this)' />&nbsp;$l_reported_posts</b></th><th><b>&nbsp;$l_reported_by </b></th><th><b>&nbsp;$l_user_ip</b></th><th>&nbsp;<b>$l_action</b></th></tr>";
          if (mysql_num_rows($get_reports) <= 0) {
              echo "<div align='center'><b>$l_no_reports</b></div>";
          } else {
              while ($row = $db->fetch_array($get_reports, 'assoc')) {
                  $post_page = $row['post_page'];
                  if ($row['post_page'] <= 1) {
                      $post_page = "1";
                  }
                  echo "<tr>
    <td width='300px' height='24px'><input type='checkbox' name='checkbox[]' value='$row[post_id]'>&nbsp;<a href='topic.php?t=$row[post_topic]&page=$post_page#$row[post_id]'>$row[post_subject]</td>
    <td>&nbsp;<a href='user.php?u=$row[user_id]'>$row[user_name]</a></td>
    <td>&nbsp;$row[user_ip]</td>
    <td>&nbsp;<a href='mcp.php?delete_report=$row[post_id]'>$l_delete</td>
    </tr>
    ";
              }
          }
          //END else
          echo "</table><br>";
          if (mysql_num_rows($get_reports) > 0) {
              echo "<input class='menuButtons' type='submit' name='Submit' value='$l_delete_selected'><br><br>";
          }
      }
      //END if reports
      //DELETE REPORT
      if (isset($_GET['delete_report'])) {
          $post_id = mysql_real_escape_string($_GET['delete_report']);
          $result = $db->query("DELETE FROM " . $table_prefix . "post_reports WHERE post_id = $post_id");
          echo "<div align='center'>$l_report_deleted<br><br><a href='mcp.php?reports=1'>$l_back_to_prev</a></div><br><br>";
      }
      //END DELETE REPORT
      
//DELETE ALL REPORTS
      if (isset($_GET['reports']) and $_GET['reports'] == 1 and $_SERVER['REQUEST_METHOD'] == 'POST') {
	     if (isset($_POST['Submit'])) {
		      for ($i = 0; $i < count($_POST['checkbox']); $i++) {
			  echo $_POST['checkbox'][$i];
              echo "<br />value $i = " . $_POST['checkbox'][$i];
              $result = $db->query("DELETE FROM " . $table_prefix . "post_reports WHERE post_id = " . $_POST['checkbox'][$i] . "");
              if ($result) {
                  header('location: mcp.php?reports=1');
              } else {
                  echo "$l_error_occured";
              }
           }//END delete post in array
		 }//END if isset POST
		 
      }//END if DELETE all reported posts
	  
//END DELETE ALL REPORTS
      
	  
	  //POST APPROVAL
if (isset($_GET['approved']) and $_GET['approved']==1) {
	  //START PAGINATION
          $result = $db->query(" SELECT COUNT(*) FROM " . $table_prefix . "posts WHERE post_approve = 1");
          $r = $db->fetch_row($result);
          $numrows = $r[0];
          // number of rows to show per page
          $rowsperpage = $topics_per_page;
          $totalpages = ceil($numrows / $rowsperpage);
          if (isset($_GET['page']) && is_numeric($_GET['page'])) {
              $currentpage = (int)$_GET['page'];
          } else {
              $currentpage = 1;
          }
          if ($currentpage > $totalpages) {
              $currentpage = $totalpages;
          }
          if ($currentpage < 1) {
              $currentpage = 1;
          }
          if ($totalpages <= 1) {
              $totalpages = 1;
          }
          $offset = ($currentpage - 1) * $rowsperpage;
          $current_page = $page;
          if ($current_page == "") {
              $current_page = 1;
          }
          $range = $max_pagination_link;
          if ($range >= $totalpages) {
              $range = $totalpages;
          }
          echo "<table><tr>";
          echo '<td align="right" class="pagination"> <b>'.$l_page.' ' . $current_page . ' '.$l_page_of.' ' . $totalpages . '</b> [' . $numrows . ' '.$l_maches.']  &nbsp; &nbsp;';
          if ($currentpage > 1) {
              echo " <a title= '$l_first_page' href='{$_SERVER['PHP_SELF']}?reports=1&page=1'><<</a> ";
              $prevpage = $currentpage - 1;
              echo " <a title='$l_prev_page' href='{$_SERVER['PHP_SELF']}?reports=1&page=$prevpage'><</a> ";
          }
          for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
              if (($x > 0) && ($x <= $totalpages)) {
                  if ($x == $currentpage) {
                      echo " [<b>$x</b>] ";
                  } else {
                      echo " <a title='$l_goto_page " . $x . "' href='{$_SERVER['PHP_SELF']}?reports=1&page=$x'>$x</a> ";
                  }
              }
          }
          if ($currentpage != $totalpages) {
              $nextpage = $currentpage + 1;
              echo " <a title='$l_next_page' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=$nextpage'>></a> ";
              echo " <a title='$l_goto_last_page' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=$totalpages'>>></a> ";
          }
		  //END PAGINATION
          echo '</td></tr></table>';
          $get_reports = $db->query("SELECT 
              " . $table_prefix . "posts.post_id,
              " . $table_prefix . "posts.post_date,
              " . $table_prefix . "posts.post_topic,
              " . $table_prefix . "posts.post_cat,
              " . $table_prefix . "posts.post_by,
              " . $table_prefix . "posts.post_subject,
              " . $table_prefix . "posts.poster_ip,
              " . $table_prefix . "posts.post_approve,
			  " . $table_prefix . "posts.post_page,
			  " . $table_prefix . "users.user_id,
			  " . $table_prefix . "users.user_ip,
			  " . $table_prefix . "users.user_name
              FROM 
              " . $table_prefix . "posts 
              LEFT JOIN 
              " . $table_prefix . "users 
              ON 
              " . $table_prefix . "users.user_id = " . $table_prefix . "posts.post_by  
              WHERE " . $table_prefix . "posts.post_approve = 1  ORDER BY 
              " . $table_prefix . "posts.post_date DESC LIMIT $offset, $post_per_page");
          echo "<script language=\"JavaScript\">
function toggle(source) {
  checkboxes = document.getElementsByName('checkbox[]');
  for(var i in checkboxes)
    checkboxes[i].checked = source.checked;
}
</script>
<form name ='myform' action='' method='post'>		  
		  <table border=1><tr><th><b><input type='checkbox' onClick='toggle(this)' />&nbsp;$l_unnaproved_posts</b></th><th><b>&nbsp;$l_post_by</b></th><th><b>&nbsp;$l_user_ip</b></th><th>&nbsp;<b>$l_action</b></th></tr>";
          if (mysql_num_rows($get_reports) <= 0) {
              echo "<div align='center'><b>$l_no_unnaproved</b></div>";
          } else {
              while ($row = $db->fetch_array($get_reports, 'assoc')) {
                  $post_page = $row['post_page'];
                  if ($row['post_page'] <= 1) {
                      $post_page = "1";
                  }
                  echo "<tr>
    <td width='300px' height='24px'><input type='checkbox' name='checkbox[]' value='$row[post_id]'>&nbsp;<a href='topic.php?t=$row[post_topic]&page=$post_page#$row[post_id]'>$row[post_subject]</td>
    <td>&nbsp;<a href='user.php?u=$row[user_id]'>$row[user_name]</a></td>
    <td>&nbsp;$row[user_ip]</td>
    <td>&nbsp;<a href='mcp.php?post_approve=$row[post_id]'>$l_approve</td>
    </tr>
    ";
              }
          }
          //END else
          echo "</table><br>";
          if (mysql_num_rows($get_reports) > 0) {
              echo "<input class='menuButtons' type='submit' name='Submit' value='$l_approve_sel'><br><br>";
          }
      }	  
	  
	 if (isset($_GET['post_approve'])) {
          $post_id = mysql_real_escape_string($_GET['post_approve']);
          $result = $db->query("UPDATE " . $table_prefix . "posts SET post_approve = 0 WHERE post_id = $post_id");
          echo "<hr><div align='center'>$l_post_approved<br><br><a href='mcp.php?approved=1'>$l_back_to_prev</a></div><br><br>";
      } 
	  
	  //APPROVE ALL REPORTS
      if (isset($_GET['approved']) and $_SERVER['REQUEST_METHOD'] == 'POST') {
	     if (isset($_POST['Submit'])) {
		      for ($i = 0; $i < count($_POST['checkbox']); $i++) {
			  echo $_POST['checkbox'][$i];
              echo "<br />value $i = " . $_POST['checkbox'][$i];
              $result = $db->query("UPDATE " . $table_prefix . "posts SET post_approve = 0 WHERE post_id = " . $_POST['checkbox'][$i] . "");
			  
              if ($result) {
                  header('location: mcp.php?post_approve=1');
              } else {
                  echo "$l_error_occured";
              }
           }//END APPROVE post in array
		 }//END if isset POST
		 
      }//END if APPROVE all unnaproved posts
	  
//END POST APPROVAL	



	  //TOPIC APPROVAL
if (isset($_GET['approve_topic'])) {
	  //START PAGINATION
          $result = $db->query(" SELECT COUNT(*) FROM " . $table_prefix . "topics WHERE topic_status = 2");
          $r = $db->fetch_row($result);
          $numrows = $r[0];
          // number of rows to show per page
          $rowsperpage = $topics_per_page;
          $totalpages = ceil($numrows / $rowsperpage);
          if (isset($_GET['page']) && is_numeric($_GET['page'])) {
              $currentpage = (int)$_GET['page'];
          } else {
              $currentpage = 1;
          }
          if ($currentpage > $totalpages) {
              $currentpage = $totalpages;
          }
          if ($currentpage < 1) {
              $currentpage = 1;
          }
          if ($totalpages <= 1) {
              $totalpages = 1;
          }
          $offset = ($currentpage - 1) * $rowsperpage;
          $current_page = $page;
          if ($current_page == "") {
              $current_page = 1;
          }
          $range = $max_pagination_link;
          if ($range >= $totalpages) {
              $range = $totalpages;
          }
          echo "<table><tr>";
          echo '<td align="right" class="pagination"> <b>'.$l_page.' ' . $current_page . ' '.$l_page_of.' ' . $totalpages . '</b> [' . $numrows . ' '.$l_maches.']  &nbsp; &nbsp;';
          if ($currentpage > 1) {
              echo " <a title= '$l_first_page' href='{$_SERVER['PHP_SELF']}?reports=1&page=1'><<</a> ";
              $prevpage = $currentpage - 1;
              echo " <a title='$l_prev_page' href='{$_SERVER['PHP_SELF']}?reports=1&page=$prevpage'><</a> ";
          }
          for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
              if (($x > 0) && ($x <= $totalpages)) {
                  if ($x == $currentpage) {
                      echo " [<b>$x</b>] ";
                  } else {
                      echo " <a title='$l_goto_page " . $x . "' href='{$_SERVER['PHP_SELF']}?reports=1&page=$x'>$x</a> ";
                  }
              }
          }
          if ($currentpage != $totalpages) {
              $nextpage = $currentpage + 1;
              echo " <a title='$l_next_page' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=$nextpage'>></a> ";
              echo " <a title='$l_goto_last_page' href='{$_SERVER['PHP_SELF']}?t=" . mysql_real_escape_string($_GET['t']) . "&page=$totalpages'>>></a> ";
          }
		  //END PAGINATION
          echo '</td></tr></table>';
          $get_reports = $db->query("SELECT 
              " . $table_prefix . "topics.topic_id,
              " . $table_prefix . "topics.topic_subject,
              " . $table_prefix . "topics.topic_date,
              " . $table_prefix . "topics.topic_cat,
              " . $table_prefix . "topics.topic_starter,
			  " . $table_prefix . "topics.topic_by 
              FROM 
              " . $table_prefix . "topics 
              WHERE " . $table_prefix . "topics.topic_status = 2 ORDER BY 
              " . $table_prefix . "topics.topic_date DESC LIMIT $offset, $post_per_page");
          echo "<script language=\"JavaScript\">
function toggle(source) {
  checkboxes = document.getElementsByName('checkbox[]');
  for(var i in checkboxes)
    checkboxes[i].checked = source.checked;
}
</script>
<form name ='myform' action='' method='post'>		  
		  <table border=1><tr><th><b><input type='checkbox' onClick='toggle(this)' />&nbsp;$l_unnaproved_topics</b></th><th><b>&nbsp;$l_post_by</b></th><th><b>&nbsp;$l_user_ip</b></th><th>&nbsp;<b>$l_action</b></th></tr>";
          if (mysql_num_rows($get_reports) <= 0) {
              echo "<div align='center'><b>$l_no_topic_unnaproved</b></div>";
          } else {
              while ($row = $db->fetch_array($get_reports, 'assoc')) {
                  if ($totalpages <= 1) {
                      $totalpages = 1;
                  }
                  echo "<tr>
    <td width='300px' height='24px'><input type='checkbox' name='checkbox[]' value='$row[topic_id]'>&nbsp;<a href='topic.php?t=$row[topic_id]&page=1'>$row[topic_subject]</td>
    <td>&nbsp;<a href='user.php?u=$row[topic_by]'>$row[topic_starter]</a></td>
    <td>&nbsp;$row[user_ip]</td>
    <td>&nbsp;<a href='mcp.php?topic_approve=$row[topic_id]'>$l_approve</td>
    </tr>
    ";
              }
          }
          //END else
          echo "</table><br>";
          if (mysql_num_rows($get_reports) > 0) {
              echo "<input class='menuButtons' type='submit' name='Submit' value='$l_approve_sel'><br><br>";
          }
      }	  
	  
	 if (isset($_GET['topic_approve'])) {
          $topic_id = mysql_real_escape_string($_GET['topic_approve']);
          $result = $db->query("UPDATE " . $table_prefix . "topics SET topic_status = 0 WHERE topic_id = $topic_id");
          echo "<hr><div align='center'>$l_post_approved<br><br><a href='mcp.php?approve_topic=1'>$l_back_to_prev</a></div><br><br>";
      } 
	  
	  //APPROVE ALL REPORTS
      if (isset($_GET['approve_topic']) and $_SERVER['REQUEST_METHOD'] == 'POST') {
	     if (isset($_POST['Submit'])) {
		      for ($i = 0; $i < count($_POST['checkbox']); $i++) {
			  echo $_POST['checkbox'][$i];
              echo "<br />value $i = " . $_POST['checkbox'][$i];
              $result = $db->query("UPDATE " . $table_prefix . "topics SET topic_status = 0 WHERE topic_id = " . $_POST['checkbox'][$i] . "");
			  
              if ($result) {
                  header('location: mcp.php?approve_topic=1');
              } else {
                  echo "$l_error_occured";
              }
           }//END APPROVE TOPIC in array
		 }//END if isset POST
		 
      }//END if APPROVE all unnaproved posts
	  
//END TOPIC APPROVAL
  
      // Get all the page's HTML into a string
      $pageContents = ob_get_contents();
      // Wipe the buffer 
      ob_end_clean();
      echo str_replace('<!--TITLE-->', $pageTitle, $pageContents);
  }
?>